<?php

namespace App\Models\Template;

use App\Models\Settle\SettleCompany;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class TemplateContractsStamp extends Model
{
    protected $table = 'template_contracts_stamp';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表 
     */
    public static function getList(Request $request, $cpid, $status = 0, $isgetall = 0)
    {
        $query = self::with(['companyinfo'])->where(['cpid' => $cpid])->orderBy('create_at', 'desc');
        if (in_array($status, [1, 2])) {
            $query->where('status', $status);
        }
        $contracts_no = $request->contracts_no;
        if ($contracts_no != '') {
            $query->where('contracts_no', 'like', '%' . $contracts_no . '%');
        }
        $company_id = $request->company_id;
        if ($company_id != '') {
            $query->where('company_id', 'like', '%' . $company_id . '%');
        }
        $company_name = $request->company_name;
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();

            return ['list' => $list];
        }
    }


    public function companyinfo(){
        return $this->hasOne(SettleCompany::class,'id','company_id');
    }
}
